/*File  1. Preps Follows data for tables and 2. Makes Table 2: list of top 10 symbols by echo-chamber (at the bottom of the do file) */

/* 	Input files: fw_new_follows, num_messages
	Output files: temp_follows, echolist_by_symbol_bear, echolist_by_symbol_bull */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************


use fw_new_follows, clear 

foreach xx of varlist _all {
	rename `xx' `=lower("`xx'")'   // "
}

foreach var of varlist bull bear {
	qui replace `var'=0 if `var'==.  
}

label var date "date of t0"
label var userid "ID of focal user"
label var bull "Focal user declares bullish in t0"
label var bear "Focal user declares bearish in t0"


	gen byte morebear_t1=cond(net_bears_1>0,1,0)
	gen byte morebull_t1=cond(net_bulls_1>0,1,0)
		label var morebear_t1 "=1 if following more bears on t+1 than on t0"
		label var morebull_t1 "=1 if following more bulls on t+1 than on t0"

gen byte f_bear_t1 =cond(net_bears_1>=net_bulls_1,1,0)  
gen byte f_bull_t1 =cond(net_bulls_1>net_bears_1,1,0)
	label var f_bear_t1 "New net follows are more bear than bull"
	label var f_bull_t1 "New net follows are more bull than bear"


gen net_bulls_t1t2=(net_bulls_1 + net_bulls_2)
gen net_bears_t1t2=(net_bears_1 + net_bears_2)
	gen byte f_bull_t1t2 =cond(net_bulls_t1t2>net_bears_t1t2,1,0)
		label var f_bull_t1t2 "New net follows t+1 to t+2 more bull than bear"

gen byte morebear_t1t2=cond(net_bears_t1t2>0,1,0)
gen byte morebull_t1t2=cond(net_bulls_t1t2>0,1,0)
	label var morebear_t1t2 "Following more bears on t+2 than on t0"
	label var morebull_t1t2 "Following more bulls on t+2 than on t0"

gen net_bulls_t1t5=(net_bulls_1 + net_bulls_2 + net_bulls_3 + net_bulls_4 + net_bulls_5)
gen net_bears_t1t5=(net_bears_1 + net_bears_2 + net_bears_3 + net_bears_4 + net_bears_5)
	gen byte f_bull_t1t5 =cond(net_bulls_t1t5>net_bears_t1t5,1,0)
		label var f_bull_t1t5 "New net follows t+1 to t+5 more bull than bear"

	clonevar cond_f_bull_t1t5 = f_bull_t1t5
		replace cond_f_bull_t1t5=. if net_bulls_t1t5==0 & net_bears_t1t5==0
		label var cond_f_bull_t1t5 "New net follows t+1 to t+5 more bull COND on >0 net follows"


foreach xx in bulls bears {
gen net_`xx'_1_10 = net_`xx'_1 + net_`xx'_2 + net_`xx'_3 + net_`xx'_4 + net_`xx'_5 + net_`xx'_6 + net_`xx'_7 + net_`xx'_8 + net_`xx'_9 + net_`xx'_10
gen net_`xx'_2_10 = net_`xx'_2 + net_`xx'_3 + net_`xx'_4 + net_`xx'_5 + net_`xx'_6 + net_`xx'_7 + net_`xx'_8 + net_`xx'_9 + net_`xx'_10
gen net_`xx'_11_30 = net_`xx'_11_20 + net_`xx'_21_30
gen net_`xx'_31_50 = net_`xx'_31_40 + net_`xx'_41_50
}

gen byte morebear_t1t5=cond(net_bears_t1t5>0,1,0)
gen byte morebull_t1t5=cond(net_bulls_t1t5>0,1,0)
	label var morebear_t1t5 "Following more bears on t+5 than on t0"
	label var morebull_t1t5 "Following more bulls on t+5 than on t0"
	
gen byte morebear_11_30=cond(net_bears_11_30>0,1,0)
gen byte morebull_11_30=cond(net_bulls_11_30>0,1,0)
	label var morebear_11_30 "Following more bears on t+30 than on t+10"
	label var morebull_11_30 "Following more bulls on t+30 than on t+10"
	
gen byte morebear_31_50=cond(net_bears_31_50>0,1,0)
gen byte morebull_31_50=cond(net_bulls_31_50>0,1,0)
	label var morebear_31_50 "Following more bears on t+50 than on t+31"
	label var morebull_31_50 "Following more bulls on t+50 than on t+31"
	

gen byte f_bull_1_10 =cond(net_bulls_1_10>net_bears_1_10,1,0) 
gen byte f_bear_1_10 =cond(net_bears_1_10>net_bulls_1_10,1,0)


foreach k in 2_10 11_30 31_50 {
gen byte f_bull_`k' =cond(net_bulls_`k'>net_bears_`k',1,0)

	clonevar cond_f_bull_`k' = f_bull_`k'
		replace cond_f_bull_`k'=. if net_bulls_`k'==0 & net_bears_`k'==0
		label var cond_f_bull_`k' "New net follows more bull COND on >0 net follows"
}


foreach xx in f_bear_t1 f_bull_t1 f_bull_t1t2 f_bull_t1t5 f_bull_2_10 f_bull_11_30 f_bull_31_50 cond_f_bull_t1t5 cond_f_bull_2_10 cond_f_bull_11_30 cond_f_bull_31_50 morebear_t1 morebull_t1 morebear_t1t5 morebull_t1t5 morebear_11_30 morebull_11_30 morebear_31_50 morebull_31_50 morebull_t1t2 morebear_t1t2 {
qui replace `xx'=`xx'*100 
}

drop net* 
save temp_follows, replace


* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 
/* --------------------------------------------------------------------------- */
* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 




/* Regressions to get Symbol FE for Table 2 */


use symbolid symbol_title comnam using permno_symbol_match_final, clear  
foreach var of varlist _all {
	rename `var' `=lower("`var'")'   // "
}
save titles, replace    
use temp_follows, clear
keep userid symbolid date bull bear f_* more*
merge m:1 symbolid using num_messages, nogen keepusing(message_rank)
save tempdata_stockFE, replace       


keep if bear==1

reghdfe morebear_t1 bear, absorb(FE=i.symbolid i.userid i.date) noconstant 
keep if e(sample)==1
keep symbolid FE message 
gduplicates drop
merge m:1 symbolid using titles, keep(match master) nogen
keep if message_rank<101
gsort -FE
cd "`=tabledir'"
save echolist_by_symbol_bear, replace
cd "`=maindir'"


use  tempdata_stockFE, clear

keep if bull==1

reghdfe morebull_t1 bull, absorb(FE=i.symbolid i.userid i.date) noconstant  
keep if e(sample)==1 
keep symbolid FE message 
gduplicates drop
merge m:1 symbolid using titles, keep(match master) nogen
keep if message_rank<101
gsort -FE
cd "`=tabledir'"
save echolist_by_symbol_bull, replace
cd "`=maindir'"

erase tempdata_stockFE.dta
erase titles.dta

